package boot.spring.service.impl;

import boot.spring.entity.Student;
import boot.spring.entity.Users;
import boot.spring.mapper.StudentMapper;
import boot.spring.mapper.UsersMapper;
import boot.spring.service.WeightmanagementService;
import boot.spring.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*分量管理的业务层*/
@Service
public class WeightmanagementServiceImpl implements WeightmanagementService {
    /*注入用户表持久层*/
    @Autowired
   private UsersMapper usersMapper;

    /*注入学生表的持久层*/
    @Autowired
    private StudentMapper studentMapper;

    /*分量*/
    @Override
    public Result fenliang() {
        /*获取到所有角色为咨询师的方法*/
        List<Users> u=usersMapper.ListMapUsersAll();
        /*获取到所有未分配咨询师的学生*/
        List<Student> stu=studentMapper.ListStuMapAll();
        Double weight=0.0;/*获取到所有咨询师权重的总和*/
        int stusize=stu.size();/*获取到所有的学生数量*/
        for(int i=0;i<u.size();i++){
            String getwei=u.get(i).getWeight();
            weight+=(double)Integer.parseInt(getwei);
        }

        for(int i=0;i<u.size()-1;i++){
            /*获取到咨询师的id*/
            Integer uid=Integer.parseInt(u.get(i).getUid());
            /*获取到当前咨询师的权重*/
            int a=(int)Integer.parseInt(u.get(i).getWeight());
            /*每个咨询师分学生的算法是(所有学生人数/权重总和)*当前咨询师的权重*/
            Integer fl=(int)Math.floor((stusize/weight)*a);

            for(int s=0;s<fl;s++){
                /*获取到学生的id*/
                Integer stuid=stu.get(0).getStuid();
                Integer wei=studentMapper.getStuUidUp(stuid,uid);
                stu.remove(0);

            }
        }

        /*获取到最后一名咨询师的id，把剩下的学生都分配给他*/
        Integer uid=Integer.parseInt(u.get(u.size()-1).getUid());
        int stulen=stu.size();/*获取到最后的学生长度*/
        for(int i=0;i<stulen;i++){
            /*获取到学生的id*/
            Integer stuid=stu.get(0).getStuid();
            /*把当前学生的id分配给最后一名咨询师*/
            Integer wei=studentMapper.getStuUidUp(stuid,uid);
            stu.remove(0);
        }

        return new Result(0,"分配成功");
    }
}
